package leetcode.day;

import java.util.*;
import java.util.stream.Collectors;

// 5990. 找出数组中的所有孤独数字
public class FindLonely {
    public static void main(String[] args) {
        int[] data = {61, 83, 92, 92, 42, 60, 16, 45, 32, 14, 40, 7, 10, 34,
                62, 33, 65, 79, 7, 14, 85, 21, 36, 5, 99, 25, 0, 14, 52, 41, 40};
        System.out.println(findLonely(data));
    }

    public static List<Integer> findLonely(int[] nums) {
        List<Integer> ans = new LinkedList<>();
        Map<Integer, Integer> map = new HashMap<>();
        // 词频统计
        for (int num : nums) {
            map.putIfAbsent(num, 0);
            map.put(num, map.get(num) + 1);
        }
        for (int num : nums) {
            if (map.get(num) == 1 && !map.containsKey(num - 1) &&
                    !map.containsKey(num + 1)) {
                ans.add(num);
            }
        }
        return ans;
    }
}
